﻿Imports VSModel
Imports Cls_Common
Partial Class Control_WhatCookToDay_Contents
    Inherits System.Web.UI.UserControl


    Public iWeeklyPlaner As Integer = 157

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("Id") Is Nothing Then
                Dim Id As Integer = CType(Page.RouteData.Values("Id"), Integer)
                LoadDetailById(Id)
                hidID.Value = Id
            Else
                LoadDetailById(0)
            End If
         
        End If
      
    End Sub
    'Load Data thuc don hang tuan
    'Sub LoadHotDealOther(ByVal iPage As Integer)
    '    Dim NumItemPerPage As Integer = 4
    '    Using data As New VSEntities
    '        Dim q = (From temp In data.Contents _
    '                    Where temp.MenuId = iWeeklyPlaner And temp.StatusId = 3 _
    '                    Select New With {temp.Contents, temp.Id, temp.Title, temp.MenuId, temp.QueryString}).ToList
    '        Dim r = q.Skip((iPage - 1) * NumItemPerPage).Take(NumItemPerPage).ToList
    '        hidCountOther.Value = q.Count
    '        If (q.Count > 0) Then
    '            Create_PagerOther(hidCountOther.Value, iPage, NumItemPerPage, NumItemPerPage)
    '        End If
    '        With rptOther
    '            .DataSource = r
    '            .DataBind()
    '        End With
    '    End Using
    'End Sub


    Sub LoadDetailById(ByVal Id As Integer)
        Using Context As New VSEntities

            If Id = 0 Then
                Dim data = (From temp In Context.Contents _
                       Where temp.MenuId = iWeeklyPlaner And temp.StatusId = 3 Order By temp.Id Descending _
                       Select temp).Skip(0).Take(1).SingleOrDefault
                If Not data Is Nothing Then

                    If (Session("PageViewDetailCook") Is Nothing) Then
                        Session("PageViewDetailCook") = Now
                        'Update so luot xem len 1
                        data.Viewed = IIf(IsNothing(data.Viewed) = True, 1, data.Viewed + 1)
                        Context.SaveChanges()
                    Else
                        If (DateDiff(DateInterval.Second, CType(Session("PageViewDetailCook"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                            'Update so luot xem len 1
                            data.Viewed = IIf(IsNothing(data.Viewed) = True, 1, data.Viewed + 1)
                            Context.SaveChanges()
                            Session("PageViewDetailCook") = Now
                        End If
                    End If
                    hplCategory.Text = IIf(IsNothing(data.Menu.Title) = True, "THỰC ĐƠN HÀNG TUẦN", data.Menu.Title)
                    hplTitle.Text = IIf(IsNothing(data.Title) = True, "", data.Title)
                    Page.Title = hplTitle.Text & " - " & HeaderTitleSSM
                    'If Not data.PublishedDate Is Nothing Then
                    '    ltrViewAndUpdateDate.Text = "Ngày cập nhật " & CType(data.PublishedDate, DateTime).ToString("dd.MM.yyyy") '"Lượt xem " & IIf(IsNothing(data.Viewed) = True, 1, data.Viewed) & "<br>
                    'Else
                    '    ltrViewAndUpdateDate.Text = "Ngày cập nhật 1/1/1900" '"Lượt xem " & IIf(IsNothing(data.Viewed) = True, 1, data.Viewed) & "<br>
                    'End If
                    Page.MetaDescription = data.Description
                    'Facebook Tag
                    'Dim metaTagTitle As New HtmlMeta
                    'metaTagTitle.Attributes.Add("property", "og:title")
                    'metaTagTitle.Attributes.Add("content", hplTitle.Text & " - " & HeaderTitleSSM)
                    'Page.Header.Controls.RemoveAt(5)
                    'Page.Header.Controls.AddAt(5, metaTagTitle)

                    'Dim metaTagUrl As New HtmlMeta
                    'metaTagUrl.Attributes.Add("property", "og:url")
                    'metaTagUrl.Attributes.Add("content", HttpContext.Current.Request.Url.AbsoluteUri)
                    'Page.Header.Controls.RemoveAt(7)
                    'Page.Header.Controls.AddAt(7, metaTagUrl)

                    'Dim metaTagImg As New HtmlMeta
                    'metaTagImg.Attributes.Add("property", "og:image")
                    'metaTagImg.Attributes.Add("content", data.Image)
                    'Page.Header.Controls.RemoveAt(8)
                    'Page.Header.Controls.AddAt(8, metaTagImg)

                    'Dim metaTagDesc As New HtmlMeta
                    'metaTagDesc.Attributes.Add("property", "og:description")
                    'metaTagDesc.Attributes.Add("content", data.Description)
                    'Page.Header.Controls.RemoveAt(10)
                    'Page.Header.Controls.AddAt(10, metaTagDesc)
                    'Show keyword
                    Dim strKeyWord As String = ""
                    Dim obj = (From ck In Context.ContentsKeywords Join k In Context.Keywords On ck.KeywordId Equals k.KeywordID Where ck.ContentId = data.Id Order By k.Keyword1 Ascending Select New With {.KeyId = ck.KeywordId, .KeyWord = k.Keyword1}).ToList
                    For i As Integer = 0 To obj.Count - 1
                        strKeyWord &= obj(i).KeyWord & ";"
                    Next

                    Page.MetaKeywords = strKeyWord
                    ' lblCountLike.Text = IIf(data.Like Is Nothing, 0, data.Like)  'CountAmountCommentOfObject(iobjId, Function_Name.Content).ToString
                    If Not data.Image Is Nothing Then
                        imgDes.ImageUrl = data.Image.ToString
                    End If

                    lnkSend.NavigateUrl = "~/Page/Member/SendLink.aspx?strtitle=" & data.Title.ToString & "&strurl=" & Request.Url.ToString & "&keepThis=true&TB_iframe=true&height=500&width=450&modal=true"

                    ltrContent.Text = IIf(IsNothing(data.Contents) = True, "", data.Contents)
                    'hpllinkFbLike.Attributes.Add("onclick", "return SetVoteContent('" & data.Id & "', '" & lblCountLike.ClientID & "');")
                    LoadNewsRelated(1, data.Id)
                    '   Comment1.CommentTypeID = Function_Name.Content
                    '   Comment1.TargetID = data.Id
                    hidID.Value = data.Id
                End If


            Else
                Dim data = (From temp In Context.Contents _
                            Where temp.MenuId = iWeeklyPlaner And temp.StatusId = 3 And temp.Id = Id _
                           Select temp).SingleOrDefault
                If Not data Is Nothing Then
                    hplTitle.Text = data.Title
                    If (Session("PageViewDetailCook") Is Nothing) Then
                        Session("PageViewDetailCook") = Now
                        'Update so luot xem len 1
                        data.Viewed = IIf(IsNothing(data.Viewed) = True, 1, data.Viewed + 1)
                        Context.SaveChanges()
                    Else
                        If (DateDiff(DateInterval.Second, CType(Session("PageViewDetailCook"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                            'Update so luot xem len 1
                            data.Viewed = IIf(IsNothing(data.Viewed) = True, 1, data.Viewed + 1)
                            Context.SaveChanges()
                            Session("PageViewDetailCook") = Now
                        End If
                    End If
                    hplCategory.Text = IIf(IsNothing(data.Menu.Title) = True, "THỰC ĐƠN HÀNG TUẦN", data.Menu.Title)
                    hplTitle.Text = IIf(IsNothing(data.Title) = True, "", data.Title)
                    Page.Title = hplTitle.Text & " - " & HeaderTitleSSM
                    ltrViewAndUpdateDate.Text = "Ngày cập nhật " & CType(data.PublishedDate, DateTime).ToString("dd.MM.yyyy") '"Lượt xem " & IIf(IsNothing(data.Viewed) = True, 1, data.Viewed) & "<br>
                    Page.MetaDescription = data.Description
                    'Show keyword
                    Dim strKeyWord As String = ""
                    Dim obj = (From ck In Context.ContentsKeywords Join k In Context.Keywords On ck.KeywordId Equals k.KeywordID Where ck.ContentId = data.Id Order By k.Keyword1 Ascending Select New With {.KeyId = ck.KeywordId, .KeyWord = k.Keyword1}).ToList
                    For i As Integer = 0 To obj.Count - 1
                        strKeyWord &= obj(i).KeyWord & ";"
                    Next
                    Page.MetaKeywords = strKeyWord
                    '   lblCountLike.Text = IIf(data.Like Is Nothing, 0, data.Like)  'CountAmountCommentOfObject(iobjId, Function_Name.Content).ToString
                    If Not data.Image Is Nothing Then
                        imgDes.ImageUrl = data.Image.ToString
                    End If

                    lnkSend.NavigateUrl = "~/Page/Member/SendLink.aspx?strtitle=" & data.Title.ToString & "&strurl=" & Request.Url.ToString & "&keepThis=true&TB_iframe=true&height=500&width=450&modal=true"

                    ltrContent.Text = IIf(IsNothing(data.Contents) = True, "", data.Contents)
                    'hpllinkFbLike.Attributes.Add("onclick", "return SetVoteContent('" & data.Id & "', '" & lblCountLike.ClientID & "');")
                    LoadNewsRelated(1, data.Id)
                    '   Comment1.CommentTypeID = Function_Name.Content
                    'Comment1.TargetID = data.Id
                End If
            End If



            'Show cac bai viet lien quan



        End Using
    End Sub


    Protected Sub LoadNewsRelated(ByVal iPage As Integer, ByVal iNewsId As Integer)
        Using data As New VSEntities
            Dim q = (From ct In data.Contents Where ct.Id <> iNewsId And ct.StatusId = 3 And ct.MenuId = iWeeklyPlaner And ct.TypeId = 1 And ct.PublishedDate <= Now _
                  And (ct.eEffectiveDate >= Now Or ct.eEffectiveDate Is Nothing) Order By ct.Created Descending Select ct).ToList
            Dim r = q.Skip((iPage - 1) * 4).Take(4).ToList
            hidCount.Value = q.Count
            If (q.Count > 0) Then
                Create_Pager(hidCount.Value, iPage, 4, 4)

            End If
            With rptArticalRelate
                .DataSource = r
                .DataBind()
            End With
        End Using
    End Sub

#Region "Bind Data bai viet lien quan"
    Protected Sub rptArticalRelate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptArticalRelate.ItemDataBound
        Dim p = CType(e.Item.DataItem, Object)
        Dim imgDes As Image = CType(e.Item.FindControl("imgDes"), Image)
        If Not p.Image Is Nothing Then
            imgDes.ImageUrl = p.Image.ToString
        End If

        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        hplImg.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = p.Title
        hplTitle.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"
        Dim ltrDes As Label = CType(e.Item.FindControl("ltrDes"), Label)
        If Not p.Description Is Nothing Then
            ltrDes.Text = p.Description.ToString
        End If

    End Sub
#End Region
#Region "Cac thu tuc phan trang bai viet lien quan"
    Protected Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        If TotalPage > 1 And Page_Index <> 1 Then
            lnkFirst.Enabled = True
        Else
            lnkFirst.Enabled = False
        End If

        If TotalPage > 1 And Page_Index <> TotalPage Then
            lnkLast.Enabled = True
        Else
            lnkLast.Enabled = False
        End If

        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
       
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        lnkTitle.CausesValidation = False
        'dang ky async ko postback
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.CssClass = "active"
            lnkTitle.Text = e.Item.DataItem
        Else
            ' lnkTitle.CssClass = "pagesLink"
            lnkTitle.Text = e.Item.DataItem
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        LoadNewsRelated(CInt(lnkTile.ToolTip.ToString), hidID.Value)

    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click

        hidCur_Page.Value = hidCur_Page.Value - 1

        LoadNewsRelated(CInt(hidCur_Page.Value), hidID.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        LoadNewsRelated(CInt(hidCur_Page.Value), hidID.Value)
    End Sub
#End Region
End Class
